﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;

namespace CPPEI.Coolzon.UI
{
    public class SwitchAnimationArgs
    {
        public PartFrame Frame { get; set; }
        public Part Part { get; set; }
        public SwitchType SwitchType { get; set; }

        public SwitchAnimationArgs(SwitchType switchType, PartFrame frame, Part part)
        {
            Frame = frame;
            Part = part;
            SwitchType = switchType;
        }
    }

    public delegate void SwitchFinish(SwitchAnimation animation, SwitchAnimationArgs args);

    public abstract class SwitchAnimation
    {
        public PartFrame Frame { get; set; }
        public Part Part { get; set; }
        public SwitchType SwitchType { get; set; }

        public SwitchAnimationState State { get; set; }

        public event SwitchFinish SwitchFinish;

        private Storyboard _storyboard;

        public void Prepare(SwitchType switchType, PartFrame frame, Part part)
        {
            _storyboard = new Storyboard();
            _storyboard.FillBehavior = FillBehavior.HoldEnd;
            _storyboard.Completed += _storyboard_Completed;
            
            SwitchType = switchType;
            Frame = frame;
            Part = part;
            OnPrepare(_storyboard);
        }

        protected abstract void OnPrepare(Storyboard storyboard);

        public void Begin()
        {
            State = SwitchAnimationState.Running;
            if (_storyboard.Children.Count == 0)
            {
                OnBegin();
                Finish();
            }
            else
            {
                _storyboard.Begin();
                OnBegin();
            }
        }

        protected abstract void OnBegin();

        public void Stop()
        {
            State = SwitchAnimationState.Stoped;

            _storyboard.Stop();
            _storyboard.Remove();
            _storyboard = null;
            Common.Logger.DebugToTag("SwitchAnimation", "移除场景:{0}的切换动画,reason:stop", Part.Name);

            OnStop();
        }

        protected abstract void OnStop();

        protected void Finish()
        {
            State = SwitchAnimationState.Finished;
            // 移除动画
            _storyboard.Remove();
            Common.Logger.DebugToTag("SwitchAnimation", "移除场景:{0}的切换动画,reason:finish", Part.Name);

            if (SwitchFinish != null)
            {
                SwitchFinish(this, new SwitchAnimationArgs(SwitchType, Frame, Part));
            }
            OnFinish();
        }

        void _storyboard_Completed(object sender, EventArgs e)
        {
            ClockGroup clockGroup = sender as ClockGroup;
            if (clockGroup.CurrentState == ClockState.Filling)
            {
                this.Finish();
            }
        }

        protected abstract void OnFinish();

        ///// <summary>
        ///// 动画非正常结束，被终止
        ///// </summary>
        //public abstract void Terminate();
    }

    public enum SwitchAnimationState
    {
        Running,
        Finished,
        Stoped,
    }
}
